<script setup>
import { onMounted, ref } from 'vue';
import { queryAllApi, addApi, queryByIdApi, updateApi, deleteByIdApi } from '@/api/dept';
import { ElMessage, ElMessageBox  } from 'element-plus';

//数据模型
const deptList = ref([]);
const dialogFormVisible = ref(false);
const dept = ref({ name: '' });
const formTitle = ref('');
const deptFormRef = ref();

//钩子函数
onMounted(() => {
  search();
})
//查询
const search = async () => {
  const result = await queryAllApi();
  //JS隐式类型转换, 数字类型: 0 == false, 其他数字 == true; 字符串类型: '', null, undefined, NaN == false,  其他 == true
  if (result.code) { 
    deptList.value = result.data;
  }
}

//新增部门
const addDept = () => {
  formTitle.value = '新增部门';
  dialogFormVisible.value = true;
  dept.value = {name: ''};
  resetForm();
}
//提交表单信息 -- 新增部门/修改部门
const save = async () => {
  //表单校验
  if (!deptFormRef.value) {
    return;
  }
  deptFormRef.value.validate(async valid => { //valid表示是否校验通过: true/false
    if (valid){ //通过
      let result;
      if (dept.value.id) { //修改部门
        result = await updateApi(dept.value);
      } else { //新增部门
        result = await addApi(dept.value);
      }

      if (result.code) { //成功
        ElMessage.success('新增部门成功');
        dialogFormVisible.value = false;
        search();
      } else {  //失败
        ElMessage.error(result.msg);
      }
    } else {  //不通过
      ElMessage.error('请填写正确的部门名称');
    }
  });
}

//表单校验
const rules = ref({
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ]
})

//重置表单的校验规则-提示信息
const resetForm = () => { 
  if (deptFormRef.value) {
    deptFormRef.value.resetFields();
  }
}

//根据ID查询部门 -- 回显
const edit = async (id) => {
  const result = await queryByIdApi(id);
  if (result.code) {
    resetForm();
    dialogFormVisible.value = true;
    formTitle.value = '修改部门';
    dept.value = result.data;
  }
}

//删除部门
const delById = async (id) => { 
  //弹出确认框
  ElMessageBox.confirm('请确认是否删除该部门, 删除操作不可撤销!','警告',
    {
      confirmButtonText: '确认删除', 
      cancelButtonText: '取消', 
      type: 'warning', 
      confirmButtonClass: 'el-button--danger', //确认按钮样式为 danger
      autofocus: false  //取消自动聚焦于确认按钮
    }
  ).then(async () => { //确认
      const result = await deleteByIdApi(id);
      if (result.code) {
        ElMessage.success('删除部门成功');
        search();
      } else {
        ElMessage.error(result.message);
      }
    }
  ).catch(() => { //取消
    ElMessage.info('取消删除部门');
  })
}
</script>

<template>
  <h1>部门管理</h1>
  <div class="container">
    <el-button type="primary" @click="addDept">+ 添加部门</el-button>
  </div>
  <!-- 表格 -->
  <div class="container">
    <el-table :data="deptList" border style="width: fit-content">
      <el-table-column type="index" label="序号" align="center" width="100" />
      <el-table-column prop="name" label="部门名称" align="center" width="260" />
      <el-table-column prop="updateTime" label="最后操作时间" align="center" width="300" />
      <el-table-column label="操作" align="center" width="300" >
        <template #default="scope">
          <el-button type="primary" @click="edit(scope.row.id)" size="small"><el-icon><Edit /></el-icon>编辑</el-button>
          <el-button type="danger" @click="delById(scope.row.id)" size="small"><el-icon><Delete /></el-icon>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

  <!-- Dialog对话框 新增部门 -->
  <el-dialog v-model="dialogFormVisible" :title="formTitle" width="500">
    <el-form :model="dept" :rules="rules" ref="deptFormRef">
      <el-form-item label="部门名称" label-width="80px" prop="name">
        <el-input v-model="dept.name" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </div>
    </template>
  </el-dialog>
  
</template>

<style scoped>
.container {
  margin: 15px 0;
}
</style>
